﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using BookSystem.CommomHelper;
using BookSystem.Model;

namespace BookSystem.BLL
{
    class User
    {

        //<==========查询操作===========>
        //按ID来查询
        public static model.User getUser(String Uid)
        {

            model.User u = new model.User();
            String strSql = "select * from T_user where Uid = @Uid";

            DataTable dt = SQLHelper.ExecuteTable(strSql, new SqlParameter("@Uid", Uid));

            if (dt.Rows.Count <= 0)
            {
                return u;

            }
            else
            {

                u.Uid = dt.Rows[0][0].ToString().Trim();
                u.Uname = dt.Rows[0][1].ToString().Trim();
                u.Pwd = dt.Rows[0][2].ToString().Trim();

                return u;

            }

        }

        //按姓名来查询ID
        public static string getUserIdByName(String Uname)
        {

            model.User u = new model.User();
            String strSql = "select Uid from T_user where Uname = @Uname";

            DataTable dt = SQLHelper.ExecuteTable(strSql, new SqlParameter("@Uname", Uname));

            if (dt.Rows.Count <= 0)
            {
                return "0";

            }
            else
            {

                u.Uid = dt.Rows[0][0].ToString().Trim();

                return u.Uid;

            }

        }


        //查询所有用户
        public static List<model.User> getAllUser()
        {
            List<model.User> users = new List<model.User>();
            String strSql = "select * from T_user";

            DataTable dt = SQLHelper.ExecuteTable(strSql);

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                model.User u = new model.User();
                u.Uid = dt.Rows[i][0].ToString().Trim();
                u.Uname = dt.Rows[i][1].ToString().Trim();
                u.Pwd = dt.Rows[i][2].ToString().Trim();
                users.Add(u);

            }

            return users;



        }

        //<==========更新操作===========>
        //更新用户信息
        public static bool updateUser(model.User u)
        {

            int i = 0;

            String strSql = "update T_user set Uname = @Uname,Pwd = @Pwd where Uid = @Uid";

            i = SQLHelper.ExecuteNonQuery(strSql,
                new SqlParameter("@Uid", u.Uid),
                new SqlParameter("@Uname", u.Uname),
                new SqlParameter("@Pwd", u.Pwd));

            return i > 0 ? true : false;
        }

        //更新用户密码
        public static bool updatePwd(model.User u)
        {

            int i = 0;

            String strSql = "update T_user set Pwd = @Pwd where Uid = @Uid";

            i = SQLHelper.ExecuteNonQuery(strSql,
                new SqlParameter("@Uid", u.Uid),
                new SqlParameter("@Pwd", u.Pwd));

            return i > 0 ? true : false;
        }

        //<==========增加操作===========>
        //增加一个用户
        public static bool addUser(model.User u)
        {

            int i = 0;
            String strSql = "insert into T_user values(@Uid, @Uname, @Pwd)";

            i = SQLHelper.ExecuteNonQuery(strSql,
                new SqlParameter("@Uid", u.Uid),
                new SqlParameter("@Uname", u.Uname),
                new SqlParameter("@Pwd", u.Pwd));


            return i > 0 ? true : false;

        }

        //<==========删除操作===========>
        public static bool delUser(String Uid) {

            int i = 0;

            String strSql = "delete T_user where Uid = @Uid";

            i = SQLHelper.ExecuteNonQuery(strSql, new SqlParameter("@Uid",Uid));

            return i > 0 ? true : false;
            
        }

        //<++++++++++验证操作++++++++++>
        //判断是否是用户
        public static bool isUser(String Uname, String Pwd) {

            String sqlStr = "select * from T_user where Uname = @Uname and Pwd = @Pwd";

            DataTable dt = SQLHelper.ExecuteTable(sqlStr, 
                new SqlParameter("@Uname", Uname), 
                new SqlParameter("@Pwd", Pwd));



            return dt.Rows.Count > 0 ? true : false;
        }

        //判断用户名是否重复
        public static bool isEquals(String Uname)
        {

            String sqlStr = "select * from T_user where Uname = @Uname";

            DataTable dt = SQLHelper.ExecuteTable(sqlStr,
                new SqlParameter("@Uname", Uname));



            return dt.Rows.Count > 0 ? true : false;
        }

    }
}
